package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.model.TbTypeTemplate;
import com.dongdongshop.model.TbTypeTemplateExample;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.SpecificationOptionService;
import com.dongdongshop.service.TypeTemplateService;
import com.dongdongshop.vo.ItemCatVO;
import com.dongdongshop.vo.SpecificationOptionVO;
import com.dongdongshop.vo.SpecificationVO;
import com.dongdongshop.vo.TypeTemplateVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TypeTemplateServiceImpl implements TypeTemplateService {
    @Resource
    private TbTypeTemplateMapper tbTypeTemplateMapper;
    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private SpecificationOptionService specificationOptionService;

    @Override
    public List<TypeTemplateVO> getTypeList(TypeTemplateVO templateVO) {
        TbTypeTemplateExample tbTypeTemplateExample = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = tbTypeTemplateExample.createCriteria();
        if (StringUtils.isNotBlank(templateVO.getName())) {
            criteria.andNameLike("%" + templateVO.getName() + "%");
        }
        List<TbTypeTemplate> tbTypeTemplates = tbTypeTemplateMapper.selectByExample(tbTypeTemplateExample);
        List<TypeTemplateVO> typeTemplateVOList = tbTypeTemplates.stream().map(type -> {
            TypeTemplateVO typeTemplateVO = new TypeTemplateVO();
            BeanUtils.copyProperties(type, typeTemplateVO);
            return typeTemplateVO;
        }).collect(Collectors.toList());
        return typeTemplateVOList;
    }

    @Override
    public void addTypeTemplate(TypeTemplateVO templateVO) {
        TbTypeTemplate typeTemplate = new TbTypeTemplate();
        BeanUtils.copyProperties(templateVO, typeTemplate);
        tbTypeTemplateMapper.insertSelective(typeTemplate);
    }

    @Override
    public TypeTemplateVO getTemplateById(Long id) {
        TbTypeTemplate typeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(id);
        TypeTemplateVO templateVO = new TypeTemplateVO();
        BeanUtils.copyProperties(typeTemplate, templateVO);
        return templateVO;
    }

    @Override
    public void updateTypeTemplate(TypeTemplateVO template) {
        TbTypeTemplate typeTemplate = new TbTypeTemplate();
        BeanUtils.copyProperties(template, typeTemplate);
        tbTypeTemplateMapper.updateByPrimaryKeySelective(typeTemplate);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        tbTypeTemplateMapper.deleteByExample(example);
    }

    @Override
    public List<TypeTemplateVO> getAllType() {
        List<TbTypeTemplate> tbTypeTemplates = tbTypeTemplateMapper.selectByExample(null);
        List<TypeTemplateVO> collect = tbTypeTemplates.stream().map(tbTypeTemplate -> {
            TypeTemplateVO typeTemplateVO = new TypeTemplateVO();
            BeanUtils.copyProperties(tbTypeTemplate, typeTemplateVO);
            return typeTemplateVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public TypeTemplateVO getTemplateByItemId(Long itemId) {
        ItemCatVO itemById = itemCatService.getItemById(itemId);
        Long typeId = itemById.getTypeId();
        TbTypeTemplate typeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(typeId);
        //获取模板中所有的规格
        String specIds = typeTemplate.getSpecIds();
        List<SpecificationVO> specificationVOList = JSONObject.parseArray(specIds, SpecificationVO.class);
        //查询选项
        //禁止for循环调用数据库,先取所有的选项
        List<SpecificationOptionVO> optionVOS = specificationOptionService.getAllOption();
        //进行分组
        Map<Long, List<SpecificationOptionVO>> groupMap = optionVOS.stream().collect(Collectors.groupingBy(optionVO -> optionVO.getSpecId()));
        for (SpecificationVO spec : specificationVOList) {
            List<SpecificationOptionVO> specIdOptinonList = groupMap.get(spec.getId());
            if (specIdOptinonList != null && !specIdOptinonList.isEmpty()) {
                spec.setList(specIdOptinonList);
            }
        }
        //再将规格放入模板VO中
        TypeTemplateVO templateVO = new TypeTemplateVO();
        templateVO.setVoList(specificationVOList);
        BeanUtils.copyProperties(typeTemplate, templateVO);
        return templateVO;
    }
}
